<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/
?>

<? /* POLL */ ?>
<?
$query = "SELECT * FROM wps_menu WHERE type='component' AND itemid = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_object ($result);
$vote_title = $row->title;

$component_id = $id;

if (isset($votenow) && $votenow==1) {
?>
<card id="Card7" title="<? echo $vote_title; ?>" ontimer="index.php?option=component&amp;id=<?php echo $component_id; ?>&amp;votenow=0">
<timer value="20"/>
<p>
<?
	if ($ans!="") {
		$query = "SELECT count FROM wps_poll_data WHERE id = $ans";
		$result = mysql_query($query);
		$row = mysql_fetch_row($result);
		$row[0]++;
		$query = "UPDATE wps_poll_data SET count = $row[0] WHERE id = $ans";
		mysql_query($query);
		echo _VOTE_THANKS;
	} else {
		echo _VOTE_SELECT;
	}
?>
</p>
<p align="center">
<anchor><? echo _BACK; ?>
<prev/>
</anchor>
</p>
</card>
<?

} else {

?>
<card id="Card7" title="<? echo $vote_title; ?>">
<p>
<b><? echo _POLL_INTRO; ?></b>
</p>
<p>
<?
if (isset($pollid)) {
	$query = "SELECT * FROM wps_poll WHERE id = $pollid";
} else {
	$query = "SELECT * FROM wps_poll WHERE published=1 ORDER BY date DESC";
}
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$pollid = $row[0];
print "<b>$row[1]</b><br />";
$query = "SELECT * FROM wps_poll_data WHERE pollid=".$pollid." ORDER BY id";
$result = mysql_query($query);

echo "<select name=\"ans\">\n";
for ($i=1;$row = mysql_fetch_row($result);$i++) {
	echo "<option value=\"".$row[0]."\">".$row[1]."</option>\n";
}
echo "</select>\n";
echo "</p>";
echo "<p>";
echo "<anchor>";
echo _VOTE_STR;
echo "<go href=\"index.php\" method=\"post\">\n";
echo "<postfield name=\"ans\" value=\"$(ans)\"/>\n";
echo "<postfield name=\"option\" value=\"component\"/>\n";
echo "<postfield name=\"id\" value=\"$component_id\"/>\n";
echo "<postfield name=\"votenow\" value=\"1\"/>\n";
echo "</go>\n";
echo "</anchor>\n";
echo "</p>";
echo "<p>";
echo "<anchor>";
echo _VOTE_RESULTS;
echo "<go href=\"#results\" method=\"post\">\n";
echo "</go>\n";
echo "</anchor>\n";
?>
</p>
<p>
<b><? echo _ALL_POLLS; ?></b><br />
<?
$query = "SELECT * FROM wps_poll WHERE published=1 ORDER BY date DESC";
$result = mysql_query($query);
for ($i=1;$row = mysql_fetch_row($result);$i++) {
	echo "<a href=\"index.php?option=component&amp;id=$component_id&amp;pollid=$row[0]\">$row[1]</a><br />\n";
}
?>
</p>
<p align="center">
<?php
echo "<anchor>"._HOME."\n";
echo "<go href=\"index.php\">\n";
echo "</go>\n";
echo "</anchor>\n";
?>
</p>
</card>
<?
}
?>
<card id="results" title="<? echo _VOTE_RESULTS; ?>">
<p>
<?
if ($pollid!="") {
	$query = "SELECT question FROM wps_poll WHERE id = $pollid";
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
	print "<b>$row[0]</b><br />\n";
	$query = "SELECT alternative, count FROM wps_poll_data WHERE pollid = $pollid ORDER BY id";
	$result = mysql_query($query);
	$total = 0;
	$poll_results = array();
	for ($i=1;$row = mysql_fetch_row($result);$i++) {
		$poll_results[$i]['alternative'] = $row[0];
		$poll_results[$i]['count'] = $row[1];
		$total += $row[1];
	}
	if ($total == 0) $total = 1;
	foreach ($poll_results as $key => $value) {
		$percent = 100*$value['count']/$total;
		$width = round($percent/2);
		echo $value['alternative'].": ".$value['count']." (".round($percent)."%)<br />\n";
		if ($pollgraphics == 1) {
			echo "<img src=\"images/poll_left.wbmp\" alt=\"\" />";
			echo "<img src=\"images/poll_bar.wbmp\" width=\"$width\" alt=\"".$value['count']."\" />";
			echo "<img src=\"images/poll_right.wbmp\" alt=\"\" />\n";
		} else {
			for ($i=0;$i<round($percent/10);$i++) echo "*";
		}
		echo "<br />\n";
	}
	echo "<br />"._VOTE_TOTAL." $total";
}
?>
</p>
<p align="center">
<anchor><? echo _BACK; ?>
<prev/>
</anchor>
</p>
</card>
<? /* END POLL */ ?>